//
//  NotesViewController.m
//  Funny
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 King. All rights reserved.
//

#import "NotesViewController.h"

@interface NotesViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arr;

@end

@implementation NotesViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self.view addSubview:self.tableView];
    UIButton *backClick = [UIButton buttonWithFrame:CGRectMake(0, 23, 60, 30) color:nil title:@" 返回" type:UIButtonTypeSystem target:nil action:@selector(backToPage) event:UIControlEventTouchUpInside cornerRadius:0];
    backClick.tintColor = [UIColor whiteColor];
    [backClick setImage:[UIImage imageNamed:@"btn_back@3x.png"] forState:UIControlStateNormal];
    UIImageView *backImg= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 66)];
    
    backImg.image = [UIImage imageNamed:@"back1.png"];
    [self.view addSubview:backImg];
    [self.view addSubview:backClick];

    // Do any additional setup after loading the view.
    
    [self listhandle];
}

- (void)backToPage
{
    [self.navigationController popViewControllerAnimated:NO];
}

- (NSMutableArray *)arr
{
    if (!_arr) {
        self.arr = [[NSMutableArray alloc]init];
    }
    return _arr;
}

- (void)listhandle
{
    NSString *str = [NSString stringWithFormat: @"http://open.qyer.com/qyer/footprint/mguide_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@&lat=38.87981972226579&lon=121.5419585079168&page=1&track_app_channel=App Store&track_app_version=6.8&track_device_info=iPhone7,2&track_deviceid=357AAE6A-2231-4843-84E7-0ED164F35BDD&track_os=ios 9.0.2&type=city&v=1",self.ID];
    str = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    [HandleData handleDataWithUrl:str completion:^(id data) {
        NSDictionary *dic1 = data;
        NSArray *arr = [dic1 objectForKey:@"data"];
        for (NSDictionary *dic in arr) {
            NotesModel *model = [NotesModel modelWithDic:dic];
            [self.arr addObject:model];
        }
        [self.tableView reloadData];
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -22, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.separatorStyle = NO;
        self.tableView.showsVerticalScrollIndicator = NO;
        [self.tableView registerClass:[NotesTableViewCell class] forCellReuseIdentifier:@"reuse8"];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse8"];
    cell.selectionStyle = UITableViewCellEditingStyleNone;
    NotesModel *model = self.arr[indexPath.row];
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning@2x.png"]];
    cell.foot.image = [UIImage imageNamed:@"image.png"];
    cell.titleLabel.text = model.title;

    [cell.userView sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"chartlet_image_traning@2x.png"]];
    cell.label1.text = @"旅行地";
    cell.countLabel.text = model.count;
    cell.titleLabel.text = model.title;
    cell.username.text = model.username;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NotesModel *model = self.arr[indexPath.row];
    NotesDetailViewController *view = [[NotesDetailViewController alloc]init];
    view.ID = model.ID;
    [self.navigationController pushViewController:view animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 270 * H;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
